<template>
  <el-dialog
    :title="paperTitle"
    :visible.sync="dialogVisible"
    :width="type === '2' ? '60%' : '30%'"
    :before-close="handleClose"
  >
    <span
      v-if="type === '1'"
    >主考试卷：<el-button type="text" style="font-size: 13px">{{
      paper
    }}</el-button></span>
    <span v-else-if="type === '2'">
      <el-table :data="lookClass" border style="width: 100%">
        <el-table-column prop="class_name" label="班级名称" width="150" />
        <el-table-column prop="main_exam_status" label="考试状态">
          <template v-slot="{ row }">
            <span style="color: #40b8ff; font-size: 14px">主考：</span>{{
              row.main_exam_status === 1
                ? "待设定时间"
                : "待考试 | " + row.exam_time_start.split('T')[0]
            }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="100">
          <template v-slot="{ row }">
            <el-button
              v-if="row.main_exam_status === 1"
              type="text"
              @click="bindClass(row.class_id, row.exam_id)"
            >解除绑定</el-button>
            <span v-else>暂无操作</span>
          </template>
        </el-table-column>
      </el-table>
    </span>
    <span v-else-if="type === '3'">
      <el-row v-for="item in classPush" :key="item.id" :gutter="10">
        <el-radio
          v-model="idPushClass"
          :label="item.id"
          style="margin-bottom: 10px; padding-left: 60px"
        >{{ item.class_name }}</el-radio>
      </el-row>
    </span>
    <span slot="footer" class="dialog-footer">
      <el-button
        v-if="type === '3'"
        type="primary"
        size="mini"
        @click="subPushclass"
      >确定</el-button>

      <el-button
        v-else
        type="primary"
        size="mini"
        @click="dialogVisible = false"
      >关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { bindClass } from '@/api/exam/exam'
export default {
  name: 'LookNmae',
  props: {
    paper: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: '1'
    },
    classPush: {
      type: Array,
      default: () => []
    },
    lookClass: {
      type: Array,
      default: () => []
    },
    pushBigId: {
      type: Number,
      default: 0
    },
    paperBigId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      dialogVisible: false,
      isshow: true, // look弹框内容
      paperTitle: '', // 弹框标题
      idPushClass: ''
    }
  },
  methods: {
    // 推送绑定
    async subPushclass() {
      if (this.idPushClass) {
        await bindClass({
          class_id: this.idPushClass,
          exam_id: this.pushBigId,
          main_paper_id: this.paperBigId
        })
        this.$emit('updateList')
        this.$message({
          message: '推送成功',
          type: 'success'
        })
        this.idPushClass = ''
        this.dialogVisible = false
      } else {
        this.dialogVisible = false
      }
      // console.log(re)
      // if (re.success === true) {
      //   return this.$message(re.msg)
      // }
    },
    // 解除绑定
    async  bindClass(classId, examId) {
      // 弹出确认框
      this.$confirm('确定解绑吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 发生解绑请求
        await bindClass({
          class_id: classId,
          del: true,
          exam_id: examId
        })
        this.dialogVisible = false// 关闭弹层
        // 调用父组件 渲染刷新
        this.$emit('cancelBind')

        this.$message({
          type: 'success',
          message: '解绑成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消解绑'
        })
      })
      // console.log('解除了')
      // this.$confirm('确定解除绑定吗?', '提示', {
      //   type: 'warning'
      // }).then(async() => {

      //   this.handleClose()
      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消'
      //   })
      // })
    },
    handleClose(done) {
      done()
    }
  }
}
</script>

<style></style>
